#This file contains the following sections of data analysis:
# 1) Main models (Appendix Section B)
# 2) Alternative dependent Variables (Appendix Section C)
# 3) Alternative independent Variables (Appendix Section D)
# 4) Squared continuous measure (Appendix Section E)
# 5) Interaction with levels of STC  (Appendix Section F)
# 6) Interaction with tradable sector size (Appendix Section G)
# 7) Additional controls  (Appendix Section H)
# 8) Investigating country-effectgs  (Appendix Section I)
# 9) Election random effects  (Appendix Section J)

# 1) Main models ####
#This section contains the models displayed in the main-text.
#We only use the trade balance measure in this section

# 1.1) Hypothesis 1 ####

m1_bin  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin + (1 | country_short)  + (1 | partyid)"),
                      data = dat)

car::linearHypothesis(m1_bin, c("stc_tba_change_binStable = stc_tba_change_binIncrease"))

# 1.1.1) Table 1 and B1 ####

texreg::texreg(list(m1_bin),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               file = "./output/Table1.tex",
               caption = "Hypothesis 1: Binned $\\Delta$ STC and $\\Delta$ incumbent vote share",
               caption.above = T,
               label = "Table1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets. Goodness of fit measures are omitted and shown in \\ref{TableB1}.}",
               digits = 2,
               dcolumn = T,
               custom.gof.names = c("N"),
               include.aic = FALSE,
               include.bic = FALSE,
               include.adjrs = FALSE,
               include.nobs = TRUE,
               include.loglik = FALSE,
               include.groups = FALSE,
               include.variance = FALSE)

texreg::texreg(list(m1_bin),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableB1.tex",
               caption = "Hypothesis 1: Binned $\\Delta$ STC and $\\Delta$ incumbent vote share",
               caption.above = T,
               label = "TableB1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 1.2) Hypothesis 2 ####

m2_bin <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * relaxed + (1 | country_short)  + (1 | partyid)"),
                     data = dat)

# 1.2.1) Figure 1 ####

marginaleffects::slopes(model = m2_bin,
                             newdata = datagrid(relaxed = c("Low", "High"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, relaxed, estimate, std.error) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         relaxed = factor(relaxed,
                          levels = c("Low", "High")),
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = relaxed, colour = relaxed, shape = relaxed)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("Clarity of responsibility:") + 
  scale_shape_discrete("Clarity of responsibility:") + 
  theme(legend.position = c(0.7, 0.85)) +
  NULL

ggsave(filename = "./output/Figure1.png",
       height = 7,
       width = 14, units = "cm")

# 1.2.2) Table B2 #### 

texreg::texreg(list(m2_bin),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 2"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableB2.tex",
               caption = "Hypothesis 2: Binned $\\Delta$ STC, clarity of responsibility, and $\\Delta$ incumbent vote share",
               caption.above = T,
               label = "TableB2",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 1.3) Hypothesis 3 ####

m3_bin <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                     data = dat)

# 1.3.1 Figure 2 ####


marginaleffects::slopes(model = m3_bin,
                             newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, lr_cat, estimate, std.error) %>% 
    mutate(lower90 = estimate - 1.645 * std.error,
           upper90 = estimate + 1.645 * std.error,
           lower95 = estimate - 1.96 * std.error,
           upper95 = estimate + 1.96 * std.error,
           contrast = factor(word(contrast, 1),
                             levels = c("Stable", "Increase")),
         lr_cat = factor(lr_cat,
                         levels = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = lr_cat)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(~lr_cat)+
  NULL

ggsave(filename = "./output/Figure2.png",
       height = 7,
       width = 14, units = "cm")

# 1.2.2) Table B3 #### 

texreg::texreg(list(m3_bin),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 3"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableB3.tex",
               caption = "Hypothesis 2: Binned $\\Delta$ STC, political ideology, and $\\Delta$ incumbent vote share",
               caption.above = T,
               label = "TableB3",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               longtable = F,
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 2) Alternative dependent variable ####
# This section contains additional analyses with a relative measures of electoral gains/losses.
# Results are shown in section C of the Appendix

# 2.1) Hypothesis 1 ####

m1_rel  <- lme4::lmer(paste("I(vote_share_change_alt*100) ~ stc_tba_change_bin + (1 | country_short)  + (1 | partyid)"),
                      data = dat)

car::linearHypothesis(m1_rel, c("stc_tba_change_binStable = stc_tba_change_binIncrease"))

# 2.1.1) Table C1 ####

texreg::texreg(list(m1_rel),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableC1.tex",
               caption = "Robustness check for hypothesis 1: Alternative dependent variable",
               caption.above = T,
               label = "TableC1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 2.2) Hypothesis 2 ####

m2_rel <- lme4::lmer(paste("I(vote_share_change_alt*100) ~ stc_tba_change_bin * relaxed + (1 | country_short)  + (1 | partyid)"),
                     data = dat)

# 2.2.1) Figure C1 ####

marginaleffects::slopes(model = m2_rel,
                        newdata = datagrid(relaxed = c("Low", "High"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, relaxed, estimate, std.error) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         relaxed = factor(relaxed,
                          levels = c("Low", "High")),
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = relaxed, colour = relaxed, shape = relaxed)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("Clarity of responsibility:") + 
  scale_shape_discrete("Clarity of responsibility:") + 
  theme(legend.position = c(0.7, 0.85)) +
  NULL

ggsave(filename = "./output/FigureC1.png",
       height = 7,
       width = 14, units = "cm")

# 2.2.2) Table C2 #### 

texreg::texreg(list(m2_rel),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 2"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableC2.tex",
               caption = "Robustness check for hypothesis 2: Alternative dependent variable",
               caption.above = T,
               label = "TableC2",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 2.3) Hypothesis 3 ####

m3_rel <- lme4::lmer(paste("I(vote_share_change_alt*100) ~ stc_tba_change_bin * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                     data = dat)

# 2.3.1) Figure C2 ####


marginaleffects::slopes(model = m3_rel,
                        newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, lr_cat, estimate, std.error) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase")),
         lr_cat = factor(lr_cat,
                         levels = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = lr_cat)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(~lr_cat)+
  NULL

ggsave(filename = "./output/FigureC2.png",
       height = 7,
       width = 16, units = "cm")

# 2.3.2) Table C2 #### 

texreg::texreg(list(m3_rel),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 3"),               
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableC3.tex",
               caption = "Robustness check for hypothesis 3: Alternative dependent variable",
               caption.above = T,
               label = "TableC3",
               use.packages = F,
               single.row = T,
               longtable = F,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 3) Alternative independent variables ####
# This section replicates the main models with different independent variables

comp_variables <- c( "stc_tba_change_bin", "stc_net_change_bin", "stc_sym_change_bin", "stc_add_change_bin")

# 3.1) Hypothesis 1 ####

m1_list <- list()

for (i in comp_variables) {
  
  m1_list[[i]] <- lme4::lmer(paste("I(vote_share_change*100) ~", i," + (1 | country_short)  + (1 | partyid)"), 
                             data = dat)
  
}

# 3.1.1) Table D1 #####

texreg::texreg(m1_list[c(2:4)],
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_net_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_net_change_binIncrease"="$\\Delta$ STC Increase",
                                      "stc_sym_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_sym_change_binIncrease"="$\\Delta$ STC Increase",
                                      "stc_add_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_add_change_binIncrease"="$\\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Net STC","Sym. STC","Add. STC"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableD1.tex",
               caption = "Robustness check for hypothesis 1: Alternative STC measures",
               caption.above = T,
               label = "TableD1",
               use.packages = F,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)


# 3.2) Hypothesis 2 ####

m2_list <- list()

for (i in comp_variables) {
  
  m2_list[[i]] <- lme4::lmer(paste("I(vote_share_change*100) ~", i, " * relaxed + (1 | country_short)  + (1 | partyid)"), 
                             data = dat)
  
}

# 3.2.1) Table D2 ####

texreg::texreg(m2_list[c(2:4)],
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_net_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_net_change_binIncrease"="$\\Delta$ STC Increase",
                                      "stc_sym_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_sym_change_binIncrease"="$\\Delta$ STC Increase",
                                      "stc_add_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_add_change_binIncrease"="$\\Delta$ STC Increase",
                                      
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      
                                      "stc_sym_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_sym_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      
                                      "stc_add_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_add_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      
                                      "stc_net_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_net_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      
                                      
                                      "(Intercept)" = NA),
               custom.model.names = c("Net STC","Sym. STC","Add. STC"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableD2.tex",
               caption = "Robustness check for hypothesis: 2: Alternative STC measures",
               caption.above = T,
               label = "TableD2",
               use.packages = F,
               float.pos = "htb",
               scalebox = .85,
               custom.note = "\\parbox{\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 3.2.2) Figure D1 ####

bind_rows(marginaleffects::slopes(model = m2_list[[3]],
                                  newdata = datagrid(relaxed = c("Low", "High"))) %>%
            select(term, contrast, comp = stc_sym_change_bin, estimate, std.error, relaxed) %>% 
            mutate(variable = "stc_sym"),
          marginaleffects::slopes(model = m2_list[[4]],
                                  newdata = datagrid(relaxed = c("Low", "High"))) %>% 
            select(term, contrast, comp = stc_add_change_bin, estimate, std.error, relaxed) %>% 
            mutate(variable = "stc_add"),
          marginaleffects::slopes(model = m2_list[[2]],
                                  newdata = datagrid(relaxed = c("Low", "High"))) %>% 
            select(term, contrast, comp = stc_net_change_bin, estimate, std.error, relaxed) %>% 
            mutate(variable = "stc_net"),
          marginaleffects::slopes(model = m2_list[[1]],
                                  newdata = datagrid(relaxed = c("Low", "High"))) %>% 
            select(term, contrast, comp = stc_tba_change_bin, estimate, std.error, relaxed) %>% 
            mutate(variable = "stc_tba")) %>%
  filter(grepl("stc_", term)) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         type = factor(ifelse(variable %in% c("stc_tba", "stc_net"), "Export and Import based", "Export based"),
                       levels = c("Export and Import based", "Export based")),
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase")),
         variable = factor(variable,
                           levels = c("stc_tba", "stc_net", "stc_sym", "stc_add"),
                           labels = c("Model 2", "Net STC", "Symmetric STC", "Additive STC"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = relaxed, colour = relaxed, shape = relaxed)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  ggh4x::facet_nested(type+variable~., scales = "free") +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("Clarity of responsibility:") + 
  scale_shape_discrete("Clarity of responsibility:") + 
  theme(legend.position = "bottom") +
  NULL

ggsave(filename = "./output/FigureD1.png",
       height = 15,
       width = 14, units = "cm")


# 3.3) Hypothesis 3 ####

m3_list <- list()

for (i in comp_variables) {
  
  m3_list[[i]] <- lme4::lmer(paste("I(vote_share_change*100) ~", i, " * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                             data = dat)
  
}


# 3.3.1) Table D3 ####

texreg::texreg(m3_list[c(2:4)],
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      
                                      "stc_sym_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_sym_change_binIncrease"="$\\Delta$ STC Increase",
                                      
                                      "stc_add_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_add_change_binIncrease"="$\\Delta$ STC Increase",
                                      
                                      "stc_net_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_net_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      
                                      "stc_sym_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_sym_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_sym_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_sym_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_sym_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_sym_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_sym_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_sym_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      
                                      "stc_add_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_add_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_add_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_add_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_add_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_add_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_add_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_add_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      
                                      "stc_net_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_net_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_net_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_net_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_net_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_net_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_net_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_net_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      
                                      
                                      "(Intercept)" = NA),
               custom.model.names = c("Net STC","Sym. STC","Add. STC"),
               file = "./output/TableD3.tex",
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               
               caption = "Robustness check for hypothesis: 3: Alternative STC measures",
               caption.above = T,
               label = "TableD3",
               use.packages = F,
               float.pos = "htb",
               longtable = T,
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)


# 3.3.2) Figure D2 ####

bind_rows(marginaleffects::slopes(model = m3_list[[3]],
                                  newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
            select(term, contrast, comp = stc_sym_change_bin, estimate, std.error, lr_cat) %>% 
            mutate(variable = "stc_sym"),
          marginaleffects::slopes(model = m3_list[[4]],
                                  newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
            select(term, contrast, comp = stc_add_change_bin, estimate, std.error, lr_cat) %>% 
            mutate(variable = "stc_add"),
          marginaleffects::slopes(model = m3_list[[2]],
                                  newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
            select(term, contrast, comp = stc_net_change_bin, estimate, std.error, lr_cat) %>% 
            mutate(variable = "stc_net"),
          marginaleffects::slopes(model = m3_list[[1]],
                                  newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
            select(term, contrast, comp = stc_tba_change_bin, estimate, std.error, lr_cat) %>% 
            mutate(variable = "stc_tba")) %>% 
  filter(grepl("stc_", term)) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         type = factor(ifelse(variable %in% c("stc_tba", "stc_net"), "Export and Import based", "Export based"),
                       levels = c("Export and Import based", "Export based")),
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase")),
         variable = factor(variable,
                           levels = c("stc_tba", "stc_net", "stc_sym", "stc_add"),
                           labels = c("Model 3", "Net STC", "Symmetric STC", "Additive STC"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = lr_cat)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  ggh4x::facet_nested(type+variable~lr_cat, scales = "free") +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
    NULL
  
ggsave(filename = "./output/FigureD2.png",
       height = 15,
       width = 14, units = "cm")

# 4) Squared continuous measure ####
#This section contains the models displayed in the main-text.
#We only use the trade balance measure in this section

# 4.1) Hypothesis 1 ####

m1  <- lme4::lmer(paste("I(vote_share_change*100) ~ (stc_tba_change + I(stc_tba_change^2)) + (1 | country_short)  + (1 | partyid)"), 
                  data = dat)

# 4.1.1) Figure E1 ####

marginaleffects::avg_slopes(m1)

marginaleffects::predictions(model = m1, re.form = NA,
                             newdata = datagrid(stc_tba_change = seq(from = -.5, to = .5, by = 0.05))) %>%
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error) %>% 
  ggplot(., aes(x = stc_tba_change, y = estimate, ymin = lower95, ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_ribbon(alpha = .25) +
  geom_ribbon(aes(ymin = lower90, ymax = upper90), alpha = .25) +
  geom_line() + 
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Predicted \u0394 vote share") + 
  NULL

ggsave(filename = "./output/FigureE1.png",
       height = 7,
       width = 14, units = "cm")

# 4.2) Hypothesis 2 ####

m2 <- lme4::lmer(paste("I(vote_share_change*100) ~ (stc_tba_change + I(stc_tba_change^2)) * relaxed + (1 | country_short)  + (1 | partyid)"),
                 data = dat)

# 4.2.1 Figure E2 ####

marginaleffects::slopes(model = m2,
                                 newdata = datagrid(relaxed = c("Low", "High"))) %>%
  filter(term == "stc_tba_change") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         relaxed = factor(relaxed,
                          levels = c("Low", "High"))) %>% 
  ggplot(., aes(x = relaxed, y = estimate, ymin = lower95, ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1) +
  theme_minimal() +
  labs(x = "\nClarity of responsibility", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  NULL

ggsave(filename = "./output/FigureE2.png",
       height = 7,
       width = 14, units = "cm")

# 4.3) Hypothesis 3 ####

m3 <- lme4::lmer(paste("I(vote_share_change*100) ~ (stc_tba_change + I(stc_tba_change^2)) * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                 data = dat)

# 4.3.1) Figure E3 ####

marginaleffects::slopes(model = m3, 
                        newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  filter(term == "stc_tba_change") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error) %>% 
  ggplot(., aes(x = lr_cat, y = estimate, ymin = lower95, ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange() + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1) +
  theme_minimal() +
  labs(x = "\nPolitical Ideology\n(Economic Left-Right Position)", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  NULL

ggsave(filename = "./output/FigureE3.png",
       height = 7,
       width = 14, units = "cm")


# 4.4) Tables of results ####

# 4.4.1) Table E1 ####

texreg::texreg(list(m1, m2, m3),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change"="$\\Delta$ STC ",
                                      "I(stc_tba_change^2)"="$\\Delta$ STC sq",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "stc_tba_change:relaxedHigh"="$\\Delta$ STC $\\times$ Clarity: High",
                                      "I(stc_tba_change^2):relaxedHigh"="$\\Delta$ STC sq $\\times$ Clarity: High",
                                      "stc_tba_change:lr_catLeft"="$\\Delta$ STC $\\times$ LW",
                                      "stc_tba_change:lr_catCenter"="$\\Delta$ STC $\\times$ Center",
                                      "stc_tba_change:lr_catRight"="$\\Delta$ STC $\\times$ RW",
                                      "stc_tba_change:lr_catFar-Right"="$\\Delta$ STC $\\times$ FR",
                                      "I(stc_tba_change^2):lr_catLeft"="$\\Delta$ STC sq $\\times$ LW",
                                      "I(stc_tba_change^2):lr_catCenter"="$\\Delta$ STC sq $\\times$ Center",
                                      "I(stc_tba_change^2):lr_catRight"="$\\Delta$ STC sq $\\times$ RW",
                                      "I(stc_tba_change^2):lr_catFar-Right"="$\\Delta$ STC sq $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1", "Model 2", "Model 3"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableE1.tex",
               caption = "Robustness check for hypotheses 1-3: Continuous measure of STC",
               caption.above = T,
               label = "TableE1",
               use.packages = F,
               float.pos = "htb",
               single.row = T,
               custom.note = "\\parbox{\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 5) Interaction with level of STC ####
# This section contains additional analysis which investigate whether the effect of STC change is a function of the STC level.

# 5.1) Hypothesis 1 ####

m1_lvl  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_prev * stc_tba_change_bin + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 5.1.1) Figure F1 ####

marginaleffects::slopes(model = m1_lvl,
                             newdata = datagrid(stc_tba_prev = c(-0.25, 0, 0.25))) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  ggplot(., aes(x = contrast,
                y = estimate, ymin = lower95, ymax = upper95,
                group = stc_tba_prev, shape = factor(stc_tba_prev), colour = factor(stc_tba_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_colour_discrete("Previous STC level") +
  scale_shape_discrete("Previous STC level") +
  NULL

ggsave(filename = "./output/FigureF1.png",
       height = 7,
       width = 14, units = "cm")


# 5.1.2) Table F1 ####

texreg::texreg(list(m1_lvl),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_prev"="Previous STC level",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "stc_tba_prev:stc_tba_change_binStable"="Previous STC level $\\times \\Delta$ STC Stable",
                                      "stc_tba_prev:stc_tba_change_binIncrease"="Previous STC level $\\times \\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableF1.tex",
               caption = "Robustness check for hypothesis 1: Interaction with absolute levels of STC",
               caption.above = T,
               label = "TableF1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 5.2) Hypothesis 2 ####


m2_lvl  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_prev * stc_tba_change_bin * relaxed + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 5.2.1) Figure F2 ####

marginaleffects::slopes(model = m2_lvl, re.form = NA,
                        newdata = datagrid(stc_tba_prev = c(-0.25, 0, 0.25),
                                           relaxed = c("Low", "High"))) %>%
  filter(term == "stc_tba_change_bin") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = relaxed, y = estimate, ymin = lower95, ymax = upper95,
                group = stc_tba_prev, colour = factor(stc_tba_prev),
                shape = factor(stc_tba_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\n Clarity of responsibility", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_shape_discrete("STC Level") +
  scale_colour_discrete("STC Level") +
  facet_grid(.~contrast) +
  NULL

ggsave(filename = "./output/FigureF2.png",
       height = 7,
       width = 14, units = "cm")

# 5.2.2) Table F2 ####

texreg::texreg(list(m2_lvl),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_prev"="Previous STC level",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "stc_tba_prev:stc_tba_change_binStable"="Previous STC level $\\times \\Delta$ STC Stable",
                                      "stc_tba_prev:stc_tba_change_binIncrease"="Previous STC level $\\times \\Delta$ STC Increase",
                                      "stc_tba_prev:relaxedHigh"="Previous STC level $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Increase $\\times$ Clarity of responsibility: High",
                                      "stc_tba_prev:stc_tba_change_binStable:relaxedHigh" = "Previous STC level $\\times \\Delta$ STC Stable $\\times$ Clarity: High",
                                      "stc_tba_prev:stc_tba_change_binIncrease:relaxedHigh" = "Previous STC level $\\times \\Delta$ STC Increase $\\times$ Clarity: High",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 2"),
               file = "./output/TableF2.tex",
               caption = "Robustness check for hypothesis 2: Interaction with absolute levels of STC",
               caption.above = T,
               label = "TableF2",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)


# 5.3) Hypothesis 3 ####

m3_lvl  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_prev * stc_tba_change_bin * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 5.3.1) Figure F3 ####

marginaleffects::slopes(model = m3_lvl, re.form = NA,
                        newdata = datagrid(stc_tba_prev = c(-0.2, 0, 0.2),
                                           lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>%
  filter(term == "stc_tba_change_bin") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = stc_tba_prev, colour = factor(stc_tba_prev),
                shape = factor(stc_tba_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\n \u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_shape_discrete("STC Level") +
  scale_colour_discrete("STC Level") +
  facet_grid(lr_cat~.) +
  NULL

ggsave(filename = "./output/FigureF3.png",
       height = 16,
       width = 14, units = "cm")

# 5.1.3.3) Table F3 ####

texreg::texreg(list(m3_lvl),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_prev"="Subnational trade competitiveness",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "stc_tba_prev:stc_tba_change_binStable"="STC $\\times$ $\\Delta$ STC",
                                      "stc_tba_prev:stc_tba_change_binIncrease"="STC $\\times$ $\\Delta$ STC sq",
                                      "stc_tba_prev:lr_catLeft"="STC $\\times$ LW",
                                      "stc_tba_prev:lr_catCenter"="STC $\\times$ Center",
                                      "stc_tba_prev:lr_catRight"="STC $\\times$ RW",
                                      "stc_tba_prev:lr_catFar-Right"="STC $\\times$ FR",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STCS table $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      "stc_tba_prev:stc_tba_change_binStable:lr_catLeft"="STC $\\times$ $\\Delta$ STC $\\times$ LW",
                                      "stc_tba_prev:stc_tba_change_binStable:lr_catCenter"="STC $\\times$ $\\Delta$ STC $\\times$ Center",
                                      "stc_tba_prev:stc_tba_change_binStable:lr_catRight"="STC $\\times$ $\\Delta$ STC $\\times$ RW",
                                      "stc_tba_prev:stc_tba_change_binStable:lr_catFar-Right"="STC $\\times$ $\\Delta$ STC $\\times$ FR",
                                      "stc_tba_prev:stc_tba_change_binIncrease:lr_catLeft"="STC $\\times$ $\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_prev:stc_tba_change_binIncrease:lr_catCenter"="STC $\\times$ $\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_prev:stc_tba_change_binIncrease:lr_catRight"="STC $\\times$ $\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_prev:stc_tba_change_binIncrease:lr_catFar-Right"="STC $\\times$ $\\Delta$ STC Increase $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 3"),
               file = "./output/TableF3.tex",
               caption = "Robustness check for hypothesis 3: Interaction with absolute levels of STC",
               longtable = T,
               caption.above = T,
               label = "TableF3",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 6) Interaction with tradable sector size ####

# 6.1) Hypothesis 1 ####

m1_tra  <- lme4::lmer(paste("I(vote_share_change*100) ~ share_trad_prev * stc_tba_change_bin + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 6.1.1) Figure G1 ####

marginaleffects::slopes(model = m1_tra,
                        newdata = datagrid(share_trad_prev = c(0.1, 0.3, 0.5))) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  ggplot(., aes(x = contrast,
                y = estimate, ymin = lower95, ymax = upper95,
                group = share_trad_prev, shape = factor(share_trad_prev), colour = factor(share_trad_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_colour_discrete("Tradable sector size:") +
  scale_shape_discrete("Tradable sector size:") +
  NULL

ggsave(filename = "./output/FigureG1.png",
       height = 7,
       width = 14, units = "cm")


# 6.1.2) Table G1 ####

texreg::texreg(list(m1_tra),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("share_trad_prev"="Size of trabable sector",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "share_trad_prev:stc_tba_change_binStable"="Size of trabable sector $\\times \\Delta$ STC Stable",
                                      "share_trad_prev:stc_tba_change_binIncrease"="Size of trabable sector $\\times \\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableG1.tex",
               caption = "Robustness check for hypothesis 1: Interaction with absolute levels of STC",
               caption.above = T,
               label = "TableG1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               #scalebox = .75,
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 7.2) Hypothesis 2 ####


m2_tra  <- lme4::lmer(paste("I(vote_share_change*100) ~ share_trad_prev * stc_tba_change_bin * relaxed + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 7.2.1) Figure G2 ####

marginaleffects::slopes(model = m2_tra, re.form = NA,
                        newdata = datagrid(share_trad_prev = c(0.1, 0.3, 0.5),
                                           relaxed = c("Low", "High"))) %>%
  filter(term == "stc_tba_change_bin") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = relaxed, y = estimate, ymin = lower95, ymax = upper95,
                group = share_trad_prev, colour = factor(share_trad_prev),
                shape = factor(share_trad_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\n Clarity of responsibility", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_colour_discrete("Tradable sector size:") +
  scale_shape_discrete("Tradable sector size:") +
  facet_grid(.~contrast) +
  NULL

ggsave(filename = "./output/FigureG2.png",
       height = 7,
       width = 14, units = "cm")

# 6.2.2) Table G2 ####

texreg::texreg(list(m2_tra),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("share_trad_prev"="Size of trabable sector",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "share_trad_prev:stc_tba_change_binStable"="Size of tradable sector $\\times \\Delta$ STC Stable",
                                      "share_trad_prev:stc_tba_change_binIncrease"="Size of tradable sector $\\times \\Delta$ STC Increase",
                                      "share_trad_prev:relaxedHigh"="Size of tradable sector $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Increase $\\times$ Clarity of responsibility: High",
                                      "share_trad_prev:stc_tba_change_binStable:relaxedHigh" = "Size of tradable sector $\\times \\Delta$ STC Stable $\\times$ Clarity: High",
                                      "share_trad_prev:stc_tba_change_binIncrease:relaxedHigh" = "Size of tradable sector $\\times \\Delta$ STC Increase $\\times$ Clarity: High",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 2"),
               file = "./output/TableG2.tex",
               caption = "Robustness check for hypothesis 2: Interaction with tradable sector size",
               caption.above = T,
               label = "TableG2",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)


# 6.3) Hypothesis 3 ####

m3_tra  <- lme4::lmer(paste("I(vote_share_change*100) ~ share_trad_prev * stc_tba_change_bin * lr_cat + (1 | country_short)  + (1 | partyid)"), 
                      data = dat)

# 6.3.1) Figure G3 ####

marginaleffects::slopes(model = m3_tra, re.form = NA,
                        newdata = datagrid(share_trad_prev = c(0.1, 0.3, 0.5),
                                           lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>%
  filter(term == "stc_tba_change_bin") %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = share_trad_prev, colour = factor(share_trad_prev),
                shape = factor(share_trad_prev))) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\n \u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  theme(legend.position = "bottom") + 
  scale_colour_discrete("Tradable sector size:") +
  scale_shape_discrete("Tradable sector size:") +
  facet_grid(lr_cat~.) +
  NULL

ggsave(filename = "./output/FigureG3.png",
       height = 16,
       width = 14, units = "cm")

# 6.1.3.3) Table G3 ####

texreg::texreg(list(m3_tra),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("share_trad_prev"="Size of trabable sector",
                                      "stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "share_trad_prev:stc_tba_change_binStable"="Tradable size $\\times$ $\\Delta$ STC",
                                      "share_trad_prev:stc_tba_change_binIncrease"="Tradable size $\\times$ $\\Delta$ STC sq",
                                      "share_trad_prev:lr_catLeft"="Tradable size $\\times$ LW",
                                      "share_trad_prev:lr_catCenter"="Tradable size $\\times$ Center",
                                      "share_trad_prev:lr_catRight"="Tradable size $\\times$ RW",
                                      "share_trad_prev:lr_catFar-Right"="Tradable size $\\times$ FR",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STCS table $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      "share_trad_prev:stc_tba_change_binStable:lr_catLeft"="Tradable size $\\times$ $\\Delta$ STC $\\times$ LW",
                                      "share_trad_prev:stc_tba_change_binStable:lr_catCenter"="Tradable size $\\times$ $\\Delta$ STC $\\times$ Center",
                                      "share_trad_prev:stc_tba_change_binStable:lr_catRight"="Tradable size $\\times$ $\\Delta$ STC $\\times$ RW",
                                      "share_trad_prev:stc_tba_change_binStable:lr_catFar-Right"="Tradable size $\\times$ $\\Delta$ STC $\\times$ FR",
                                      "share_trad_prev:stc_tba_change_binIncrease:lr_catLeft"="Tradable size $\\times$ $\\Delta$ STC Increase $\\times$ LW",
                                      "share_trad_prev:stc_tba_change_binIncrease:lr_catCenter"="Tradable size $\\times$ $\\Delta$ STC Increase $\\times$ Center",
                                      "share_trad_prev:stc_tba_change_binIncrease:lr_catRight"="Tradable size $\\times$ $\\Delta$ STC Increase $\\times$ RW",
                                      "share_trad_prev:stc_tba_change_binIncrease:lr_catFar-Right"="Tradable size $\\times$ $\\Delta$ STC Increase $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 3"),
               file = "./output/TableG3.tex",
               caption = "Robustness check for hypothesis 3: Interaction with tradable sector size",
               longtable = T,
               caption.above = T,
               label = "TableG3",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 7) Additional controls ####
#This section adds gnic per capita for to the model.

# 7.1) Hypothesis 1 ####

m1_con  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin + gnic_growth + (1 | country_short)  + (1 | partyid)"),
                      data = dat)

# 7.1.1) Table H1 ####

texreg::texreg(list(m1_con),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "gnic_growth" = "GNI per capita growth",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableH1.tex",
               caption = "Robustness check for hypothesis 1: Controlling for GNI per capita growth",
               caption.above = T,
               label = "TableH1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 7.2) Hypothesis 2 ####

m2_con <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * relaxed + gnic_growth + (1 | country_short)  + (1 | partyid)"),
                     data = dat)

# 7.2.1) Figure H1 ####

marginaleffects::slopes(model = m2_con,
                        newdata = datagrid(relaxed = c("Low", "High"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, relaxed, estimate, std.error) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         relaxed = factor(relaxed,
                          levels = c("Low", "High")),
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = relaxed, colour = relaxed, shape = relaxed)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("Clarity of responsibility:") + 
  scale_shape_discrete("Clarity of responsibility:") + 
  theme(legend.position = c(0.7, 0.85)) +
  NULL

ggsave(filename = "./output/FigureH1.png",
       height = 7,
       width = 14, units = "cm")

# 7.2.2) Table H2 #### 

texreg::texreg(list(m2_con),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "relaxedHigh"="Clarity of responsibility: High",
                                      "gnic_growth" = "GNI per capita growth",
                                      "stc_tba_change_binStable:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "stc_tba_change_binIncrease:relaxedHigh"="$\\Delta$ STC Stable $\\times$ Clarity of responsibility: High",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 2"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableH2.tex",
               caption = "Robustness check for hypothesis 2: Controlling for GNI per capita growth",
               caption.above = T,
               label = "TableH2",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.8\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 7.3) Hypothesis 3 ####

m3_con <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * lr_cat + gnic_growth + (1 | country_short)  + (1 | partyid)"), 
                     data = dat)

# 7.3.1) Figure H2 ####

marginaleffects::slopes(model = m3_con,
                        newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  filter(term == "stc_tba_change_bin") %>% 
  select(contrast, lr_cat, estimate, std.error) %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         contrast = factor(word(contrast, 1),
                           levels = c("Stable", "Increase")),
         lr_cat = factor(lr_cat,
                         levels = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  ggplot(., aes(x = contrast, y = estimate, ymin = lower95, ymax = upper95,
                group = lr_cat)) +
  geom_hline(yintercept = 0, lty = "dotted") + 
  geom_line(position = position_dodge(width = .5)) +
  geom_pointrange(position = position_dodge(width = .5)) + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1, position = position_dodge(width = .5)) +
  theme_minimal() +
  labs(x = "\u0394 Subnational trade competitiveness", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(~lr_cat)+
  NULL

ggsave(filename = "./output/FigureH2.png",
       height = 7,
       width = 14, units = "cm")

# 7.2.2) Table H3 #### 

texreg::texreg(list(m3_con),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "lr_catLeft"="Ideology: Left-wing",
                                      "lr_catCenter"="Ideology: Center",
                                      "lr_catRight"="Ideology: Right-wing",
                                      "lr_catFar-Right"="Ideology: Far-right",
                                      "gnic_growth" = "GNI per capita growth",
                                      "stc_tba_change_binStable:lr_catLeft"="$\\Delta$ STC Stable $\\times$ LW",
                                      "stc_tba_change_binIncrease:lr_catLeft"="$\\Delta$ STC Increase $\\times$ LW",
                                      "stc_tba_change_binStable:lr_catCenter"="$\\Delta$ STC Stable $\\times$ Center",
                                      "stc_tba_change_binIncrease:lr_catCenter"="$\\Delta$ STC Increase $\\times$ Center",
                                      "stc_tba_change_binStable:lr_catRight"="$\\Delta$ STC Stable $\\times$ RW",
                                      "stc_tba_change_binIncrease:lr_catRight"="$\\Delta$ STC Increase $\\times$ RW",
                                      "stc_tba_change_binStable:lr_catFar-Right"="$\\Delta$ STC Stable $\\times$ FR",
                                      "stc_tba_change_binIncrease:lr_catFar-Right"="$\\Delta$ STC Increase $\\times$ FR",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 3"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Country", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableH3.tex",
               caption = "Robustness check for hypothesis 3: Controlling for GNI per capita growth",
               caption.above = T,
               label = "TableH3",
               use.packages = F,
               single.row = T,
               longtable = F,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the country- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)

# 8) Investigating country-effects ####

# 8.1) Jackknifing ####

country_list <- unique(dat$country_short) 

dat_jack_m1 <- data.frame(country_short = as.character(),
                          stc_tba_change_bin = as.character(),
                          estimate = as.numeric(),
                          std.error = as.numeric())

dat_jack_m2 <- data.frame(country_short = as.character(),
                          stc_tba_change_bin = as.character(),
                          relaxed = as.character(),
                          estimate = as.numeric(),
                          std.error = as.numeric())

dat_jack_m3 <- data.frame(country_short = as.character(),
                          stc_tba_change_bin = as.character(),
                          lr_cat = as.character(),
                          estimate = as.numeric(),
                          std.error = as.numeric())

for (i in country_list){
  
  
  m1_temp  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin + (1 | country_short)  + (1 | partyid)"), 
                         data = dat %>% filter(country_short != i))
  
  m2_temp <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * relaxed + (1 | country_short)  + (1 | partyid)"),
                        data = dat %>% filter(country_short != i))
  
  m3_temp <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin * lr_cat +  (1 | country_short)  + (1 | partyid)"),
                        data = dat %>% filter(country_short != i))
  
  out_temp_m1 <- marginaleffects::avg_slopes(model = m1_temp) %>% 
    data.frame() %>% 
    mutate(contrast = factor(word(contrast, 1),
                             levels = c("Stable", "Increase"))) %>% 
    select(stc_tba_change_bin = contrast, estimate, std.error) %>% 
    mutate(country_short = i)
  
  dat_jack_m1 <- bind_rows(dat_jack_m1, out_temp_m1)
  
  out_temp_m2 <- marginaleffects::slopes(model = m2_temp,
                                         newdata = datagrid(relaxed = c("Low", "High"))) %>%
    filter(term == "stc_tba_change_bin") %>% 
    data.frame() %>%
    mutate(contrast = factor(word(contrast, 1),
                             levels = c("Stable", "Increase"))) %>% 
    select(stc_tba_change_bin = contrast, relaxed, estimate, std.error) %>% 
    mutate(country_short = i)
  
  dat_jack_m2 <- bind_rows(dat_jack_m2, out_temp_m2)
  
  out_temp_m3 <- marginaleffects::slopes(model = m3_temp,
                                         newdata = datagrid(lr_cat = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>%
    filter(term == "stc_tba_change_bin") %>% 
    data.frame() %>%
    mutate(contrast = factor(word(contrast, 1),
                             levels = c("Stable", "Increase"))) %>% 
    select(stc_tba_change_bin = contrast, lr_cat, estimate, std.error) %>% 
    mutate(country_short = i)
  
  dat_jack_m3 <- bind_rows(dat_jack_m3, out_temp_m3)
  
}

# 8.1.1) Figure I1 ####

dat_jack_m1 %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         country_short = forcats::fct_rev(country_short),
         stc_tba_change_bin = factor(stc_tba_change_bin,
                                     levels = c("Stable", "Increase"))) %>% 
  ggplot(., aes(x = country_short, y = estimate,
                        ymin = lower95,
                        ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted")+
  geom_pointrange() + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1) +
  theme_minimal() +
  labs(x = "Dropped country", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(~stc_tba_change_bin)+
  coord_flip()+
  NULL

ggsave(filename = "./output/FigureI1.png",
       height = 14,
       width = 14, units = "cm")


# 8.1.2) Figure I2 ####

dat_jack_m2 %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         country_short = forcats::fct_rev(country_short),
         stc_tba_change_bin = factor(stc_tba_change_bin,
                                     levels = c("Stable", "Increase")),
         relaxed = factor(relaxed, 
                          levels = c("Low", "High"))) %>% 
  ggplot(., aes(x = country_short, y = estimate,
                ymin = lower95,
                ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted")+
  geom_pointrange() + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1) +
  theme_minimal() +
  labs(x = "Dropped country", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(stc_tba_change_bin~relaxed, scales = "free")+
  coord_flip()+
  NULL

ggsave(filename = "./output/FigureI2.png",
       height = 20,
       width = 20, units = "cm")

# 8.1.3) Figure I3 ####

dat_jack_m3 %>% 
  mutate(lower90 = estimate - 1.645 * std.error,
         upper90 = estimate + 1.645 * std.error,
         lower95 = estimate - 1.96 * std.error,
         upper95 = estimate + 1.96 * std.error,
         country_short = forcats::fct_rev(country_short),
         stc_tba_change_bin = factor(stc_tba_change_bin,
                                     levels = c("Stable", "Increase")),
         lr_cat = factor(lr_cat, 
                          levels = c("Far-Left", "Left", "Center", "Right", "Far-Right"))) %>% 
  ggplot(., aes(x = country_short, y = estimate,
                ymin = lower95,
                ymax = upper95)) +
  geom_hline(yintercept = 0, lty = "dotted")+
  geom_pointrange() + 
  geom_linerange(aes(ymin = lower90, ymax = upper90), linewidth = 1) +
  theme_minimal() +
  labs(x = "Dropped country", y = "Marginal effect of \n\u0394 STC on \u0394 incumbent vote share") + 
  scale_colour_discrete("System:") + 
  scale_shape_discrete("System:") + 
  theme(legend.position = "none") +
  facet_grid(stc_tba_change_bin~lr_cat, scales = "free")+
  coord_flip()+
  NULL

ggsave(filename = "./output/FigureI3.png",
       height = 20,
       width = 20, units = "cm")

# 9) Election random effects ####

dat$country_year <- paste0(dat$country_short, dat$year)

# 9.1) Hypothesis 1 ####

m1_ele  <- lme4::lmer(paste("I(vote_share_change*100) ~ stc_tba_change_bin + (1 | country_year)  + (1 | partyid)"),
                      data = dat)

# 9.1.1) Table J1 ####

texreg::texreg(list(m1_ele),
               stars = c(.1, .05, .01),
               leading.zero = T,
               custom.coef.map = list("stc_tba_change_binStable"="$\\Delta$ STC Stable",
                                      "stc_tba_change_binIncrease"="$\\Delta$ STC Increase",
                                      "(Intercept)" = NA),
               custom.model.names = c("Model 1"),
               custom.gof.names = c("AIC", "BIC", "Log Likelihood", "N", "Group: Parties", "Group: Elections", "Var. Party", "Var. Country", "Var. Residuals"),
               file = "./output/TableJ1.tex",
               caption = "Robustness check for hypothesis 1: Random effects on elections",
               caption.above = T,
               label = "TableJ1",
               use.packages = F,
               single.row = T,
               float.pos = "htb",
               custom.note = "\\parbox{.5\\linewidth}{%stars. Entries are unstandardised coefficients from a linear mixed-effects model with random intercepts at the election- and party-level. Standard errors in brackets.}",
               digits = 2,
               dcolumn = T)
